<template>
  <div class="com-container">
    <div class="com-chart"
         ref="sellerRef"></div>
  </div>
</template>

<script>
import { getSellerData } from 'network/seller.js';
export default {
  name: 'Seller',
  data () {
    return {
      echartsInstance: null,
      allData: [], //服务器返回的数据
      currentPage: 1, //当前显示的页数
      totalPage: 1, //总共页数
      timer: null, //定时器标识
    }
  },
  mounted () {
    this.initChart();
    this.getData();
    this.adaptScreen();
    window.addEventListener('resize', this.adaptScreen);
  },
  destroyed () {
    clearInterval(this.timer);
    window.removeEventListener('resize', this.adaptScreen);
  },
  methods: {
    //初始化echarts实例化对象
    initChart () {
      this.echartsInstance = this.$echarts.init(this.$refs.sellerRef, 'chalk');
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: '▎商家销售统计',
          left: 20,
          top: 20,
        },
        grid: {
          top: '18%',
          left: '3%',
          right: '6%',
          bottom: '5%',
          containLabel: true //grid 区域包含坐标轴的刻度标签
        },
        //提示框组件
        tooltip: {
          trigger: 'axis', //坐标轴触发,
          //坐标轴指示器配置项
          axisPointer: {
            type: 'line',  // 直线指示器
            z: 0,
            lineStyle: {
              color: '#2d3443',
              type: 'solid'
            }
          }
        },
        xAxis: {
          type: 'value'
        },
        yAxis: {
          type: 'category',
        },
        series: [
          {
            type: "bar",
            label: {
              show: true,
              position: 'right',
            },
            itemStyle: {
              //echart 渐变内置生成器echarts.graphic.LinearGradient
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                //百分之0状态下的颜色值
                {
                  offset: 0,
                  color: '#5052ee'
                },
                //百分之100状态下的颜色值
                {
                  offset: 1,
                  color: '#ab6ee5'
                }
              ])
            }
          }
        ]
      };
      this.echartsInstance.setOption(initOption);
      //对图表对象进行鼠标事件的监听
      this.echartsInstance.on('mouseover', () => {
        clearInterval(this.timer);
      });
      this.echartsInstance.on('mouseout', () => {
        this.startInterval();
      })
    },

    //获取服务器的数据
    async getData () {
      const { data: res } = await getSellerData();
      this.allData = res;
      //对数据进行排序
      this.allData.sort((a, b) => {
        return a.value - b.value; //从小到大
      });
      this.totalPage = Math.ceil(this.allData.length / 5);
      this.updateChart();
    },

    //更新图表
    updateChart () {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end); //要展示的数据
      const sellerName = showData.map((item) => {
        return item.name
      });
      const sellerValue = showData.map((item) => {
        return item.value;
      });
      const dataOption = {
        yAxis: {
          data: sellerName
        },
        series: [
          {
            data: sellerValue,
          }
        ]
      };
      this.echartsInstance.setOption(dataOption);

    },

    startInterval () {
      if (this.timer) {
        clearInterval(this.timer);
      }
      this.timer = setInterval(() => {
        this.currentPage < this.totalPage ? this.currentPage++ : this.currentPage = 1;
        this.updateChart();
      }, 6000)
    },

    //浏览器窗口发生变化，调用
    adaptScreen () {
      //console.log(this.$refs.sellerRef.offsetWidth);
      const titleFontSize = this.$refs.sellerRef.offsetWidth / 100 * 3.6;
      //和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        //提示框组件
        tooltip: {
          //坐标轴指示器配置项
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            }
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              borderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0], //（顺时针左上，右上，右下，左下）
            }
          }
        ]
      };
      this.echartsInstance.setOption(adapterOption);
      this.echartsInstance.resize();
    }

  },
}
</script>

<style lang="less" scoped>
</style>